package com.huizhonglm.diap.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.NinePatchDrawable;
import android.util.AttributeSet;
import android.widget.ImageView;

/**
 * 鍦嗗舰imageView锛堜笉甯﹁竟妗嗭級
 * 
 * @author LiXiaolong
 * @since 2014骞�鏈�鏃ヤ笅鍗�:38:41
 * @version 1.0
 */
public class RoundImageView extends ImageView {
	// default color(wiate)
	private int defColor = 0x00000000;
	// border thickness
	private int thickness = 1;
	// border outside color
	private int outsideColor = 0x00000000;
	// border inside color
	private int insideColor = 0x00000000;
	// default width
	private int defWidth = 0;
	// default height
	private int defHeight = 0;

	public RoundImageView(Context context) {
		super(context);
	}

	public RoundImageView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public RoundImageView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		Drawable drawable = getDrawable();
		if (drawable == null) {
			return;
		}

		if (getWidth() == 0 || getHeight() == 0) {
			return;
		}
		this.measure(0, 0);
		if (drawable.getClass() == NinePatchDrawable.class)
			return;
		Bitmap b = ((BitmapDrawable) drawable).getBitmap();
		Bitmap bitmap = b.copy(Config.ARGB_8888, true);
		if (defWidth == 0) {
			defWidth = getWidth();

		}
		if (defHeight == 0) {
			defHeight = getHeight();
		}
		// 淇濊瘉閲嶆柊璇诲彇鍥剧墖鍚庝笉浼氬洜涓哄浘鐗囧ぇ灏忚�鏀瑰彉鎺т欢瀹姐�楂樼殑澶у皬锛堥拡瀵瑰銆侀珮涓簑rap_content甯冨眬鐨刬mageview锛屼絾浼氬鑷磎argin鏃犳晥锛�
		// if (defaultWidth != 0 && defHeight != 0) {
		// LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
		// defaultWidth, defHeight);
		// setLayoutParams(params);
		// }
		int radius = 0;
		if (insideColor != defColor && outsideColor != defColor) {// 瀹氫箟鐢讳袱涓竟妗嗭紝鍒嗗埆涓哄鍦嗚竟妗嗗拰鍐呭渾杈规
			radius = (defWidth < defHeight ? defWidth : defHeight) / 2 - 2 * thickness;
			// 鐢诲唴鍦�
			drawCircleBorder(canvas, radius + thickness / 2, insideColor);
			// 鐢诲鍦�
			drawCircleBorder(canvas, radius + thickness + thickness / 2, outsideColor);
		} else if (insideColor != defColor && outsideColor == defColor) {// 瀹氫箟鐢讳竴涓竟妗�
			radius = (defWidth < defHeight ? defWidth : defHeight) / 2 - thickness;
			drawCircleBorder(canvas, radius + thickness / 2, insideColor);
		} else if (insideColor == defColor && outsideColor != defColor) {// 瀹氫箟鐢讳竴涓竟妗�
			radius = (defWidth < defHeight ? defWidth : defHeight) / 2 - thickness;
			drawCircleBorder(canvas, radius + thickness / 2, outsideColor);
		} else {// 娌℃湁杈规
			radius = (defWidth < defHeight ? defWidth : defHeight) / 2;
		}
		Bitmap roundBitmap = getCroppedRoundBitmap(bitmap, radius);
		canvas.drawBitmap(roundBitmap, defWidth / 2 - radius, defHeight / 2 - radius, null);
	}

	/**
	 * 鑾峰彇瑁佸壀鍚庣殑鍦嗗舰鍥剧墖
	 * 
	 * @param radius
	 *            鍗婂緞
	 */
	public Bitmap getCroppedRoundBitmap(Bitmap bmp, int radius) {
		Bitmap scaledSrcBmp;
		int diameter = radius * 2;
		// 涓轰簡闃叉瀹介珮涓嶇浉绛夛紝閫犳垚鍦嗗舰鍥剧墖鍙樺舰锛屽洜姝ゆ埅鍙栭暱鏂瑰舰涓浜庝腑闂翠綅缃渶澶х殑姝ｆ柟褰㈠浘鐗�
		int bmpWidth = bmp.getWidth();
		int bmpHeight = bmp.getHeight();
		int squareWidth, squareHeight, x, y;
		Bitmap squareBitmap;
		if (bmpHeight > bmpWidth) {// 楂樺ぇ浜庡
			squareWidth = squareHeight = bmpWidth;
			x = 0;
			y = (bmpHeight - bmpWidth) / 2;
			// 鎴彇姝ｆ柟褰㈠浘鐗�
			squareBitmap = Bitmap.createBitmap(bmp, x, y, squareWidth, squareHeight);
		} else if (bmpHeight < bmpWidth) {// 瀹藉ぇ浜庨珮
			squareWidth = squareHeight = bmpHeight;
			x = (bmpWidth - bmpHeight) / 2;
			y = 0;
			squareBitmap = Bitmap.createBitmap(bmp, x, y, squareWidth, squareHeight);
		} else {
			squareBitmap = bmp;
		}

		if (squareBitmap.getWidth() != diameter || squareBitmap.getHeight() != diameter) {
			scaledSrcBmp = Bitmap.createScaledBitmap(squareBitmap, diameter, diameter, true);

		} else {
			scaledSrcBmp = squareBitmap;
		}
		Bitmap output = Bitmap.createBitmap(scaledSrcBmp.getWidth(), scaledSrcBmp.getHeight(),
				Config.ARGB_8888);
		Canvas canvas = new Canvas(output);

		Paint paint = new Paint();
		Rect rect = new Rect(0, 0, scaledSrcBmp.getWidth(), scaledSrcBmp.getHeight());

		paint.setAntiAlias(true);
		paint.setFilterBitmap(true);
		paint.setDither(true);
		canvas.drawARGB(0, 0, 0, 0);
		canvas.drawCircle(scaledSrcBmp.getWidth() / 2, scaledSrcBmp.getHeight() / 2,
				scaledSrcBmp.getWidth() / 2, paint);
		paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
		canvas.drawBitmap(scaledSrcBmp, rect, rect, paint);
		// bitmap鍥炴敹(recycle瀵艰嚧鍦ㄥ竷灞�枃浠禭ML鐪嬩笉鍒版晥鏋�
		// bmp.recycle();
		// squareBitmap.recycle();
		// scaledSrcBmp.recycle();
		bmp = null;
		squareBitmap = null;
		scaledSrcBmp = null;
		return output;
	}

	/**
	 * 杈圭紭鐢诲渾
	 */
	private void drawCircleBorder(Canvas canvas, int radius, int color) {
		Paint paint = new Paint();
		/* 鍘婚敮榻�*/
		paint.setAntiAlias(true);
		paint.setFilterBitmap(true);
		paint.setDither(true);
		paint.setColor(color);
		/* 璁剧疆paint鐨勩�style銆�负STROKE锛氱┖蹇�*/
		paint.setStyle(Paint.Style.STROKE);
		/* 璁剧疆paint鐨勫妗嗗搴�*/
		paint.setStrokeWidth(thickness);
		canvas.drawCircle(defWidth / 2, defHeight / 2, radius, paint);
	}

}
